import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:local_auth/local_auth.dart';
import 'package:local_auth/error_codes.dart' as auth_error;

class EMIFaceIDManger {
  factory EMIFaceIDManger() => _instance;
  static final EMIFaceIDManger _instance = EMIFaceIDManger._internal();

  final LocalAuthentication auth = LocalAuthentication();
  EMIFaceIDManger._internal() {}

  Future<bool> authenticate() async {
    bool authenticated = false;
    try {
      authenticated = await auth.authenticate(
        localizedReason: '请使用Face ID进行身份验证',
        options: const AuthenticationOptions(
          useErrorDialogs: true,
          stickyAuth: true,
        ),
      );
      return authenticated;
    } on PlatformException catch (e) {
      if (e.code == auth_error.notAvailable) {
        debugPrint(
            'EMIFaceIDManger --  Biometric authentication is not available on this device.');
      } else if (e.code == auth_error.notEnrolled) {
        debugPrint('EMIFaceIDManger -- No biometric credentials are enrolled.');
      } else {
        debugPrint('EMIFaceIDManger -- Unknown error: $e');
      }
      return false;
    }
  }
}
